﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.ENT;
using SIS.BLL;

public partial class student_student_registration : System.Web.UI.Page
{
    SIS.UI.Common _common = new SIS.UI.Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Params["id"] != null)
            {
                int _id = -1;
                int _srid = -1;
                if (Int32.TryParse(Request.Params["id"].ToString(), out _id))
                {
                    hplStudent.NavigateUrl = "~/student/?id=" + _id.ToString();
                    ddlSchoolList.DataBind();

                    _common.LoadRelationshipTypeDDL(ddlGSRelation, string.Empty);

                    if (LoadStudentInfo(_id))
                    {
                        if (Request.Params["srid"] != null && Int32.TryParse(Request.Params["srid"].ToString(), out _srid))
                        {
                            StudentRegisterationTable _register = StudentRegisterationTableManager.GetItem(_srid);
                            if (_register != null && _register.SRID > 0)
                            {
                                lblSRID.Text = _register.SRID.ToString();
                                ddlSchoolList.Text = _register.SchoolKey.ToString();
                                ddlSchoolList.Enabled = false;

                                ddlSchoolMajor.DataBind();
                                ddlSchoolMajor.Text = _register.SchoolMajorKey.ToString();


                                ddlStandardLevel.DataBind();
                                ddlStandardLevel.Text = _register.StandardLevelKey.ToString();

                                ddlLocation.DataBind();
                                ddlLocation.Text = _register.LocationKey.ToString();

                                ddlPeriod.DataBind();
                                ddlPeriod.Text = _register.EducationPeriodKey.ToString();

                                ddlClassRoom.Text = _register.ClassRoomKey.ToString();
                                txtRemark.Text = _register.Remark;
                                lblEditRegistration.Text = "1";
                            }
                            else
                            {

                                ddlSchoolMajor.DataBind();
                                ddlStandardLevel.DataBind();
                                ddlLocation.DataBind();
                                ddlPeriod.DataBind();
                                ddlClassRoom.DataBind();
                                CheckRegiterStatus();
                                lblEditRegistration.Text = "0";
                            }
                        }
                        else
                        {

                            ddlSchoolMajor.DataBind();
                            ddlStandardLevel.DataBind();
                            ddlLocation.DataBind();
                            ddlPeriod.DataBind();
                            ddlClassRoom.DataBind();
                            CheckRegiterStatus();
                            lblEditRegistration.Text = "0";
                        }
                    }
                    else
                    {
                        btnSubmit.Enabled = false;
                        lblEditRegistration.Text = "0";
                    }
                }
                else
                {
                    lblTopMessage.Text = "Invalid student account ID.";
                }

                //lblTopMessage.Text = DateTime.Now.ToString();
            }
            else
            {
                //lblTopMessage.Text = DateTime.Now.ToString();
                Response.Redirect("student-list.aspx");
            }
        }
    }

    private void CheckRegiterStatus()
    {
        //bool _retValue = false;

        if (lblEditRegistration.Text == "1")
        {
            //_retValue = false;
        }
        else
        {
            int _schoolKey = 0;// Convert.ToInt32(ddlSchoolList.Text);
            int _eduPeriodKey = 0;//Convert.ToInt32(ddlPeriod.Text);
            int _studentProfileKey = 0;//Convert.ToInt32(lblStudentProfileKey.Text);
            int _standardLevelKey = 0;// Convert.ToInt32(ddlStandardLevel.Text);
            int _userAccountKey = 0;//Convert.ToInt32(lblUserAccountKey.Text);
            int _classRoomKey = 0;// Convert.ToInt32(ddlClassRoom.Text);

            if (Int32.TryParse(ddlSchoolList.Text, out _schoolKey) &&
                Int32.TryParse(ddlPeriod.Text, out _eduPeriodKey) &&
                Int32.TryParse(lblStudentProfileKey.Text, out _studentProfileKey) &&
                    Int32.TryParse(ddlStandardLevel.Text, out _standardLevelKey) &&
                        Int32.TryParse(lblUserAccountKey.Text, out _userAccountKey) &&
                        Int32.TryParse(ddlClassRoom.Text, out _classRoomKey))
            {
                StudentRegisterationTable _register = StudentRegisterationTableManager.GetItem(_schoolKey, _userAccountKey, _studentProfileKey, _eduPeriodKey, _standardLevelKey, _classRoomKey);
                if (_register != null && _register.SRID > 0)
                {
                    lblSRID.Text = _register.SRID.ToString();
                    ddlTimeTable.DataBind();
                    if (ddlTimeTable.Items.Count > 0)
                    {
                        ddlTimeTable.Text = _register.TimeTableKey.ToString();
                        ddlTimeTable_SelectedIndexChanged(null, null);
                    }
                    txtRemark.Text = _register.Remark;
                    lblTopMessage.Text = "Found student registeration record!";
                }
                else
                {
                    lblSRID.Text = "-1";
                    txtRemark.Text = "";
                    lblTopMessage.Text = "New registeration for this student!";
                }

                btnSubmit.Enabled = true;
            }
            else
            {
                lblTopMessage.Text = "No engoush school's information found.";
                btnSubmit.Enabled = false;
            }
        }

        //return _retValue;
    }

    private bool LoadStudentInfo(int AccountID)
    {
        bool _retValue = true;
        UserAccountTable _account = new UserAccountTable();
        _account = UserAccountTableManager.GetItem(AccountID);

        if (_account == null)
        {
            lblTopMessage.Text = "Student account ID (" + AccountID.ToString() + ") is not found!";
            hplStudent.Visible = false;
            _retValue = false;
        }
        else if (_account.UserAccountKey <= 0)
        {
            lblTopMessage.Text = "Student account ID is not found!";
            hplStudent.Visible = false;
            _retValue = false;
        }

        if (_account != null && _account.UserAccountKey > 0)
        {
            if (_account.UserAccountTypeCode != 300)
            {
                lblTopMessage.Text = "Only for student account ID will be loaded.";
                hplStudent.Visible = false;
                _retValue = false;
            }

            lblUserAccountKey.Text = _account.UserAccountKey.ToString();

            StudentProfileTable _sprofile = new StudentProfileTable();
            StudentAddressTable _saddress = new StudentAddressTable();
            StudentContactTable _scontact = new StudentContactTable();
            StudentGuardianRelationshipTable _gsrelation = new StudentGuardianRelationshipTable();
            ddlSchoolList.Text = _account.SchoolKey.ToString();
            ddlSchoolList.Enabled = false;

            _sprofile = StudentProfileTableManager.GetItemByUserAccountKey(AccountID);
            if (_sprofile != null && _sprofile.StudentProfileKey > 0)
            {
                lblStudentProfileKey.Text = _sprofile.StudentProfileKey.ToString();

                //txtFirstName.Text = _sprofile.FirstName;
                //txtLastName.Text = _sprofile.LastName;
                //txtOtherName.Text = _sprofile.OtherName;
                string _fullName = string.Empty;
                if (_sprofile.FirstName.Length > 0)
                {
                    _fullName = _sprofile.FirstName;
                }

                if (_sprofile.LastName.Length > 0)
                {
                    _fullName += " " + _sprofile.LastName;
                }

                if (_sprofile.OtherName.Length > 0)
                {
                    _fullName += " &lt;or&gt; " + _sprofile.OtherName;
                }
                //lblFullName.Text = _fullName;
                //_fullName = " - " + _sprofile.Gender + " ";

                DateTime _dtDOB = _sprofile.DateOfBirth;
                int _age = DateTime.Now.Year - _dtDOB.Year;
                _fullName += " ( " + _sprofile.Gender + ", " + _age.ToString() + " yrs )";
                lblFullName.Text = _fullName;
                //ddlDOBDay.Text = _dtDOB.Day.ToString("00");
                //ddlDOBMonth.Text = _dtDOB.Month.ToString("00");
                //ddlDOBYear.Text = _dtDOB.Year.ToString("0000");

                //txtNRICNumber.Text = _sprofile.NRICNumber;
                //ddlDrivingLicenceType.Text = _sprofile.DrivingLicenceType.ToString();
                //txtDrivingLicenceNumber.Text = _sprofile.DrivingLicenseNumber;

                //ddlTravelDocType.Text = _sprofile.TravelDocumentType.ToString();
                //txtTravelDocNumber.Text = _sprofile.TravelDocumentNumber;

                //ddlBloodGroup.Text = _sprofile.BloodGroupCode.ToString();
                //ddlMaritalStatus.Text = _sprofile.MartialStatusCode.ToString();
                //ddlRaceCode.Text = _sprofile.RaceCode.ToString();
                //ddlReligion.Text = _sprofile.ReligionCode.ToString();
                //ddlNationality.Text = _sprofile.NationalityCode.ToString();
                //txtWebsiteUrl.Text = _sprofile.WebSiteUrl;
                //txtDisablityRemark.Text = _sprofile.DisabilityRemark;
                //txtRemark.Text = _sprofile.Remark;
            }
            else
            {
                _retValue = false;
            }

            _saddress = StudentAddressTableManager.GetItemByUserAccountKey(AccountID);
            if (_saddress != null && _saddress.StudentAddressKey > 0)
            {
                //lblStudentAddressKey.Text = _saddress.StudentAddressKey.ToString();
                //txtUnitNo.Text = _saddress.UnitNo;
                //txtStreetName.Text = _saddress.StreetName;
                //ddlCity.Text = _saddress.CityCode.ToString();
                //ddlDistrict.Text = _saddress.DistrictCode.ToString();
                //ddlCountry.Text = _saddress.CountryCode.ToString();
                //txtPostCode.Text = _saddress.PostalCode;
            }
            else
            {
                _retValue = false;
            }

            _scontact = StudentContactTableManager.GetItemByUserAccountKey(AccountID);
            if (_scontact != null && _scontact.StudentContactKey > 0)
            {
                string _contactInfo = "<ul>";
                if (_scontact.MobileNumber != null && _scontact.MobileNumber.Length > 0)
                {
                    _contactInfo = "<li>Mobile: " + _scontact.MobileNumber + "<li>";
                }

                if (_scontact.HomePhoneNumber != null && _scontact.HomePhoneNumber.Length > 0)
                {
                    _contactInfo += "<li>Home: " + _scontact.HomePhoneNumber + "</li>";
                }

                if (_scontact.OtherNumber != null && _scontact.OtherNumber.Length > 0)
                {
                    //if (_contactInfo.Length > 0)
                    //    _contactInfo += ", ";

                    _contactInfo += "<li>Other: " + _scontact.OtherNumber + "</li>";
                }

                if (_scontact.EmailAddress != null && _scontact.EmailAddress.Length > 0)
                {
                    //if (_contactInfo.Length > 0)
                    //    _contactInfo += ", ";

                    _contactInfo += "<li>Email: " + _scontact.EmailAddress + "</li>";
                }
                _contactInfo += "</ul>";

                lblContactInfo.Text = _contactInfo;
                //lblStudentContactKey.Text = _scontact.StudentContactKey.ToString();
                //txtMobileNumber.Text = _scontact.MobileNumber;
                //txtHomePhoneNumber.Text = _scontact.HomePhoneNumber;
                //txtEmailAddress.Text = _scontact.EmailAddress;
                //txtOtherContactNumber.Text = _scontact.OtherNumber;
            }
            else
            {
                _retValue = false;
            }

            _gsrelation = StudentGuardianRelationshipTableManager.GetItemByStudentAccountKey(AccountID, true);

            if (_gsrelation != null && _gsrelation.GuardianAccountKey > 0)
            {

                //lblStudentGuardianRelationTypeKey.Text = _gsrelation.GSAutoID.ToString();
                UserAccountTable _gaccount = new UserAccountTable();
                _gaccount = UserAccountTableManager.GetItem(_gsrelation.GuardianAccountKey);

                if (_gaccount != null && _gaccount.UserAccountKey > 0)
                {
                    //lblGuardianAccountKey.Text = _gaccount.UserAccountKey.ToString();
                    GuardianProfileTable _gprofile = new GuardianProfileTable();
                    GuardianAddressTable _gaddress = new GuardianAddressTable();
                    GuardianContactTable _gcontact = new GuardianContactTable();

                    _gprofile = GuardianProfileTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gprofile != null && _gprofile.GuardianProfileKey > 0)
                    {
                        string _fullName = string.Empty;
                        if (_gprofile.FirstName.Length > 0)
                        {
                            _fullName = _gprofile.FirstName;
                        }

                        if (_gprofile.LastName.Length > 0)
                        {
                            _fullName += " " + _gprofile.LastName;
                        }

                        if (_gprofile.OtherName != null && _gprofile.OtherName.Length > 0)
                        {
                            _fullName += " ( " + _gprofile.OtherName + " )";
                        }

                        ddlGSRelation.Text = _gsrelation.RelationshipTypeCode.ToString();
                        ddlGSRelation.Enabled = false;
                        lblGuardianFullName.Text = _fullName;
                        //lblGuardianProfileKey.Text = _gprofile.GuardianProfileKey.ToString();
                        //txtGuardianFirstName.Text = _gprofile.FirstName;
                        //txtGuardianLastName.Text = _gprofile.LastName;
                        DateTime _dtGDOB = _gprofile.DateOfBirth;
                        //ddlGuardianDOBDay.Text = _dtGDOB.Day.ToString("00");
                        //ddlGuardianDOBMonth.Text = _dtGDOB.Month.ToString("00");
                        //ddlGuardianDOBYear.Text = _dtGDOB.Year.ToString("0000");
                        //ddlGuardianRelationshipType.Text = _gsrelation.RelationshipTypeCode.ToString();
                    }

                    _gaddress = GuardianAddressTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey, 1);
                    if (_gaddress != null && _gaddress.GuardianAddressKey > 0)
                    {
                        //lblGuardianAddressKey.Text = _gaddress.GuardianAddressKey.ToString();
                        //txtGuardianAddressUnitNo.Text = _gaddress.UnitNo;
                        //txtGuardianAddressStreet.Text = _gaddress.StreetName;
                        //txtGuardianAddressPostal.Text = _gaddress.PostalCode;
                        //ddlGuardianAddressCountry.Text = _gaddress.CountryCode.ToString();
                    }

                    _gcontact = GuardianContactTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gcontact != null && _gcontact.GuardianContactKey > 0)
                    {
                        //lblGuardianContactKey.Text = _gcontact.GuardianContactKey.ToString();
                        //txtGuardianHomePhoneNumber.Text = _gcontact.HomePhoneNumber;
                        //txtGuardianMobileNumber.Text = _gcontact.MobileNumber;
                        string _contactInfo = "<ul>";
                        if (_gcontact.MobileNumber != null && _gcontact.MobileNumber.Length > 0)
                        {
                            _contactInfo = "<li>Mobile: " + _gcontact.MobileNumber + "<li>";
                        }

                        if (_gcontact.HomePhoneNumber != null && _gcontact.HomePhoneNumber.Length > 0)
                        {
                            _contactInfo += "<li>Home: " + _gcontact.HomePhoneNumber + "</li>";
                        }
                        _contactInfo += "</ul>";
                        lblGuardianContactInfo.Text = _contactInfo;
                    }
                }
                else
                {
                    _retValue = false;
                }
            }
            else
            {
                _retValue = false;
            }
        }
        return _retValue;
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnShowSubject_Click(object sender, EventArgs e)
    {

    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlStandardLevel.DataBind();
        ddlPeriod.DataBind();
        ddlClassRoom.DataBind();
        CheckRegiterStatus();
    }

    protected void ddlPeriod_SelectedIndexChanged(object sender, EventArgs e)
    {
        CheckRegiterStatus();
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        CheckRegiterStatus();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        ddlClassRoom.DataBind();
        CheckRegiterStatus();
    }

    protected void ddlClassRoom_SelectedIndexChanged(object sender, EventArgs e)
    {
        CheckRegiterStatus();
        ddlTimeTable.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid && Convert.ToInt32(lblUserAccountKey.Text) > 0 && Convert.ToInt32(lblStudentProfileKey.Text) > 0)
        {
            StudentRegisterationTable _register = new StudentRegisterationTable();
            _register.SRID = Convert.ToInt32(lblSRID.Text);
            _register.UserAccountKey = Convert.ToInt32(lblUserAccountKey.Text);
            _register.StudentProfileKey = Convert.ToInt32(lblStudentProfileKey.Text);
            _register.EducationPeriodKey = Convert.ToInt32(ddlPeriod.Text);
            _register.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
            _register.StandardLevelKey = Convert.ToInt32(ddlStandardLevel.Text);
            _register.SchoolMajorKey = Convert.ToInt32(ddlSchoolMajor.Text);
            _register.LocationKey = Convert.ToInt32(ddlLocation.Text);
            _register.ClassRoomKey = Convert.ToInt32(ddlClassRoom.Text);
            _register.TimeTableKey = Convert.ToInt32(ddlTimeTable.Text);
            _register.Remark = txtRemark.Text;
            _register.Status = true;
            StudentRegisterationTableManager.Save(_register);
            if (_register.SRID > 0)
            {
                lblSRID.Text = _register.SRID.ToString();
                lblTopMessage.Text = "Saving succeeded.";
            }
            else
            {
                lblTopMessage.Text = "Saving failed.";
            }
        }
        else
        {
            lblTopMessage.Text = "Validation failed!";
        }

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }

    //protected void ddlTimeTable_DataBound(object sender, EventArgs e)
    //{
    //    if (ddlTimeTable.Items.Count > 0)
    //    {
    //        ddlTimeTable.SelectedIndex = 0;
    //        ddlTimeTable_SelectedIndexChanged(sender, e);
    //    }
    //    else
    //    {
    //        hplViewTimeTableDetail.Visible = false;
    //    }
    //}

    protected void ddlTimeTable_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTimeTable.Items.Count > 0)
        {
            hplViewTimeTableDetail.NavigateUrl = "~/school/timetable-details.aspx?ttid=" + ddlTimeTable.Text + "&clsrmid=" + ddlClassRoom.Text;
            hplViewTimeTableDetail.Visible = true;
        }
        else
        {
            hplViewTimeTableDetail.Visible = false;
        }
    }
}
